package com.sky.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Slf4j
@Configuration
public class RedisConfiguration {

    // 定义 RedisTemplate 的 Bean
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        log.info("开始创建 RedisTemplate 对象...");
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 设置redis的连接工厂对象
        template.setConnectionFactory(redisConnectionFactory);

        // 设置序列化方式
        template.setKeySerializer(new StringRedisSerializer());// 设置key的序列化方式，不设置到redis中key值会乱码
//        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());// 设置value的序列化方式，不设置到redis中value值会乱码

        return template;
    }
}
